package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysTreeDictDataMapper;
import com.ruoyi.system.mapper.SysTreeDictTypeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectMapper;
import com.ruoyi.system.service.IProjectService;

/**
 * 工程管理Service业务层处理
 * 
 * @author genghz
 * @date 2021-12-18
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private SysTreeDictDataMapper sysTreeDictDataMapper ;

    @Autowired
    private SysTreeDictTypeMapper sysTreeDictTypeMapper ;

    /**
     * 查询工程管理
     * 
     * @param taskId 工程管理主键
     * @return 工程管理
     */
    @Override
    public Project selectProjectByTaskId(Long taskId)
    {
        return projectMapper.selectProjectByTaskId(taskId);
    }

    /**
     * 查询工程管理列表
     * 
     * @param project 工程管理
     * @return 工程管理
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        List<Project> projects = projectMapper.selectProjectList(project);

        for (Project param : projects) {
            if(!Objects.isNull(param.getSysLel1())) {
                SysTreeDictData sysTreeDictData1 = sysTreeDictDataMapper.selectSysTreeDictDataByDataId(param.getSysLel1());
                param.setSysLel1Name(sysTreeDictData1.getDictLabel());
            }
            if(!Objects.isNull(param.getSysLel2())) {
                SysTreeDictData sysTreeDictData2 = sysTreeDictDataMapper.selectSysTreeDictDataByDataId(param.getSysLel2());
                param.setSysLel2Name(sysTreeDictData2.getDictLabel());
            }
            if(!Objects.isNull(param.getSysLel3())) {
                SysTreeDictData sysTreeDictData3 = sysTreeDictDataMapper.selectSysTreeDictDataByDataId(param.getSysLel3());
                param.setSysLel3Name(sysTreeDictData3.getDictLabel());
            }
            if(!Objects.isNull(param.getDependId())) {
                Project task = projectMapper.selectProjectByTaskId(param.getTaskId());
                param.setDependName(task.getNodeTask());
            }
        }

        return projects ;
    }

    /**
     * 新增工程管理
     * 
     * @param project 工程管理
     * @return 结果
     */
    @Override
    public int insertProject(Project project)
    {
        for (int i = 0; i < project.getSysLel().length; i++) {
            switch (i) {
                case 0:
                    project.setSysLel1(project.getSysLel()[i]);
                    break;
                case 1:
                    project.setSysLel2(project.getSysLel()[i]);
                    break;
                case 2:
                    project.setSysLel3(project.getSysLel()[i]);
                    break;
            }
        }
        int i = projectMapper.insertProject(project);
        return i;
    }

    /**
     * 修改工程管理
     * 
     * @param project 工程管理
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        for (int i = 0; i < project.getSysLel().length; i++) {
            switch (i) {
                case 0:
                    project.setSysLel1(project.getSysLel()[i]);
                    break;
                case 1:
                    project.setSysLel2(project.getSysLel()[i]);
                    break;
                case 2:
                    project.setSysLel3(project.getSysLel()[i]);
                    break;
            }
        }
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除工程管理
     * 
     * @param taskIds 需要删除的工程管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectByTaskIds(Long[] taskIds)
    {
        return projectMapper.deleteProjectByTaskIds(taskIds);
    }

    /**
     * 删除工程管理信息
     * 
     * @param taskId 工程管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectByTaskId(Long taskId)
    {
        return projectMapper.deleteProjectByTaskId(taskId);
    }

    @Override
    public List<String> getDistinctSystem(Long planId) {
        return projectMapper.getDistinctSystem(planId);
    }

    @Override
    public List<String> getDistinctSubSystem(String sysLel1) {
        return projectMapper.getDistinctSubSystem(sysLel1);
    }

    @Override
    public List<String> getDistinctDept() {
        return projectMapper.getDistinctDept();
    }

    @Override
    public List<Project> getDistinctTask(Long planId) {
        return projectMapper.getDistinctTask(planId);
    }

    @Override
    public String importTask(List<Project> list, Long planId) {

        List<SysTreeDictData> system = getSystem();
        for (Project project : list) {
            project.setPlanId(planId);
            project.setSysLel1(getSystemDataId(system, project.getSysLel1Name()));
            project.setSysLel2(getSystemDataId(system, project.getSysLel2Name()));
            project.setSysLel3(getSystemDataId(system, project.getSysLel3Name()));
            project.setMilestone("是".equalsIgnoreCase(project.getMilestoneName())?"1":"0");
            projectMapper.insertProject(project);
        }
        for (Project project : list) {
            if(StringUtils.isNotEmpty(project.getDependName())) {
                String dependName = project.getDependName();
                Long parentTaskId = getParentTaskId(list, dependName);
                project.setDependId(parentTaskId);
                projectMapper.updateProject(project);
            }
        }

        return null ;
    }

    public Long getParentTaskId(List<Project> projects, String taskName) {
        return projects.stream().filter(x -> x.getNodeTask().equalsIgnoreCase(taskName)).findFirst().orElse(new Project()).getTaskId();
    }

    public Long getSystemDataId(List<SysTreeDictData> sysTreeDictDatas, String dataLabel) {
        return sysTreeDictDatas.stream().filter(x -> x.getDictLabel().equalsIgnoreCase(dataLabel)).findFirst().orElse(new SysTreeDictData()).getDataId();
    }

    public List<SysTreeDictData> getSystem() {
        SysTreeDictType sysTreeDictType = new SysTreeDictType();
        sysTreeDictType.setDictType("plan_sys");
        List<SysTreeDictType> sysTreeDictTypes = sysTreeDictTypeMapper.selectSysTreeDictTypeList(sysTreeDictType);
        SysTreeDictType sysTreeDictType1 = sysTreeDictTypes.get(0);
        SysTreeDictData param = new SysTreeDictData();
        param.setDictId(sysTreeDictType1.getDictId());
        List<SysTreeDictData> sysTreeDictData = sysTreeDictDataMapper.selectSysTreeDictDataList(param);
        return sysTreeDictData ;
    }

    @Override
    public Gantt gantt(Long planId) {
        Long startId = 10000000000L ;
        Gantt gantt = new Gantt();

        List<GanttData> ganttDatas = new ArrayList<>();
        Map<Long,GanttData> treeGanttDatas = new HashMap<>();
        List<SysTreeDictData> systems = getSystem();
        for (SysTreeDictData system : systems) {
            GanttData ganttData = new GanttData();
            ganttData.setId(system.getDataId());
            ganttData.setText(system.getDictLabel());
            ganttData.setParent(system.getParentId());
            ganttData.setOpen(true);
            ganttData.setType("project");
            treeGanttDatas.put(system.getDataId(), ganttData);
        }

        ganttDatas.addAll(treeGanttDatas.values());

        List<GanttData> taskGanttDatas = new ArrayList<>();
        List<Project> tasks = projectMapper.getDistinctTask(planId);
        for (Project task : tasks) {
            GanttData ganttData = new GanttData();
            ganttData.setId(task.getTaskId());
            ganttData.setText(task.getNodeTask());
            ganttData.setStart_date(task.getStartDate());
            ganttData.setStatus(Integer.valueOf(task.getStatus()));
            Long days = (task.getEndDate().getTime() - task.getStartDate().getTime())/1000/24/60/60 ;
            ganttData.setDuration(days);
            Long parent = ObjectUtils.firstNonNull(task.getSysLel3(), task.getSysLel2(), task.getSysLel1());
            GanttData ganttData1 = treeGanttDatas.get(parent);
            if(!Objects.isNull(ganttData1)) {
                ganttData.setParent(ganttData1.getId());
            }
            taskGanttDatas.add(ganttData);
        }
        ganttDatas.addAll(taskGanttDatas);

        List<GanttLinks> ganttDataList = new ArrayList<>();
        List<Project> ganttLinks = projectMapper.getGanttLinks(planId);
        for (Project project : ganttLinks) {
            GanttLinks link = new GanttLinks();
            link.setId(startId++);
            link.setSource(project.getDependId());
            link.setTarget(project.getTaskId());
            link.setType("0");
            ganttDataList.add(link);
        }

        Collections.sort(ganttDatas, new Comparator<GanttData>() {
            @Override
            public int compare(GanttData o1, GanttData o2) {
                Long l = o1.getId() - o2.getId();
                return l.intValue();
            }
        });

        gantt.setData(ganttDatas);
        gantt.setLinks(ganttDataList);
        return gantt ;
    }
}
